package com.xiaozhengkeji.springboot.frame.easy.api;

import com.alibaba.fastjson.JSON;
import com.xiaozhengkeji.springboot.frame.easy.Bean.QueryInfo;
import com.xiaozhengkeji.springboot.frame.easy.Bean.ResponseBean;
import com.xiaozhengkeji.springboot.frame.easy.entity.SysUser;
import com.xiaozhengkeji.springboot.frame.easy.service.IUserService;
import com.xiaozhengkeji.springboot.frame.easy.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserApi {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private IUserService iUserService;

    @PostMapping("/login")
    public ResponseBean login(@RequestParam("username") String username,
                              @RequestParam("password") String password) throws AuthenticationException {
        return new ResponseBean(200, "ok", iUserService.login(username, password));
    }

    @PostMapping("/register")
    public ResponseBean login(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              @RequestParam("email") String email) throws AuthenticationException {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        sysUser.setEMail(email);
        sysUser.setRegDate(new Date());
        return new ResponseBean(200, "ok", iUserService.register(sysUser));
    }


    @GetMapping(value = "/refreshToken")
    @PreAuthorize("hasRole('ROLE_def')")
    public String refreshToken(@RequestHeader String Token) throws AuthenticationException {
        return iUserService.refreshToken(Token);
    }


    @GetMapping(value = "/all")
    @PreAuthorize("hasRole('ROLE_admin')")
    public ResponseBean all(QueryInfo queryInfo) throws AuthenticationException {
        int numbers = sysUserService.queryAll(new SysUser()).size();// 获取数据总数
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize(); //页码
        List<SysUser> users = sysUserService.queryAllByLimit(pageStart, queryInfo.getPageSize());
        return new ResponseBean(numbers, "ok", users);
    }

    @PutMapping("/editUser")
    @PreAuthorize("hasRole('ROLE_admin')")
    public ResponseBean editUser(@RequestBody SysUser sysUser,
                                 @RequestHeader String Token) {
        log.info(JSON.toJSONString(sysUser));
        SysUser sysUser1 = sysUserService.update(sysUser);
        if (sysUser1 != null) {
            return new ResponseBean(200, "success", null);
        }
        return new ResponseBean(500, "error", null);
    }

    @DeleteMapping("/deleteUser")
    @PreAuthorize("hasRole('ROLE_admin')")
    public ResponseBean deleteUser(int id,
                                   @RequestHeader String Token) {
        if (sysUserService.deleteById(id)) {

            return new ResponseBean(200, "success", null);
        }
        return new ResponseBean(500, "error", null);
    }

    @GetMapping("/getUpdate")
    @PreAuthorize("hasRole('ROLE_admin')")
    public ResponseBean getUpdateUser(int id) {
        return new ResponseBean(200, "ok", sysUserService.queryById(id));
    }
}
